import QQMapWX from "./map/qqmap-wx-jssdk.min.js"
import {mapkey} from "./config.js"
var qqmapsdk = new QQMapWX({
    key: mapkey // 必填
}); 
export default function(location = {}) {
	return new Promise((success, fail) => {
		var d = Object.keys(location);
		if(d.length == 0){
			uni.getLocation({
				type: 'gcj02',
				geocode : true,
				success: res => {
					location = {
						latitude : res.latitude,
						longitude : res.longitude
					};
					//获取到坐标后进行逆地址解析
					qqmapsdk.reverseGeocoder({
					      location: location || '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
					      //get_poi: 1, //是否返回周边POI列表：1.返回；0不返回(默认),非必须参数
					      success: function(res) {//成功后的回调
					        //console.log(res,location);
							location['address'] = res.result.address;
							location['address_detail'] = res.result.address_component;
							uni.setStorageSync('location',location);
					        success(res)
						},
						fail:function(res){
							success(res) 
						}
					})
				},
				fail: () => {
					uni.showModal({
						title: '温馨提示',
						content: '无法获取你的位置信息。请到手机系统的[设置]->[隐私]->[定位服务]中打开定位服务，并允许微信使用定位服务。',
						confirmText: "确认",
						showCancel: false,
						success: res => {
							if (res.confirm) {
								uni.openSetting({
								  success(res) {
								    console.log(res)
									uni.getLocation({
										type: 'gcj02',
										geocode : true,
										success: res => {
											qqmapsdk.reverseGeocoder({
											      location: location || '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
											      //get_poi: 1, //是否返回周边POI列表：1.返回；0不返回(默认),非必须参数
											      success: function(res) {//成功后的回调
											        //console.log(res,location);
													location['address'] = res.result.address;
													location['address_detail'] = res.result.address_component;
													uni.setStorageSync('location',location);
											        success(res)
												},
												fail:function(res){
													success(res) 
												}
											})
										},
									})
								  }
								});
							}
						}
					});
				},
				complete: () => {}
			});
		}else{
			qqmapsdk.reverseGeocoder({
		      location: location || '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
		      //get_poi: 1, //是否返回周边POI列表：1.返回；0不返回(默认),非必须参数
		      success: function(res) {//成功后的回调
		       // console.log(res,location);
			   location['address'] = res.result.address;
			   location['address_detail'] = res.result.address_component;
			   uni.setStorageSync('location',location);
		        success(res)
			},
			fail:function(res){
				success(res) 
			}
		})
		}
		
	});
}


